<?php
/**
 * XSS Filter - Variable
 *
 * <code>
 * $iInt = utilXssHtml::filterInt(1111);
 * </code>
 *
 * @package util
 * @subpackage xss
 *
 * @author jylee3@simplexi.com
 * @version 1.0
 */
class utilXssVar
{
    /**
     * Integer Filter
     *
     * <code>
     * $iInt = utilXssVar::filterInt($mInt);
     * </code>
     *
     * @param    Mixed    $mInt
     *
     * @return    Integer
     */
    public static function filterInt($mInt)
    {
        return (int)$mInt;
    }

    /**
     * Float Filter
     *
     * <code>
     * $iFloat = utilXssVar::filterFloat($mFloat);
     * </code>
     *
     * @param    Mixed    $mFloat
     *
     * @return    Float
     */
    public static function filterFloat($mFloat)
    {
        return (float)$mFloat;
    }

    /**
     * Alphabetics Filter
     *
     * <code>
     * $sAlphabetics = utilXssVar::filterFloat('Alphabetics');
     * </code>
     *
     * @param    String    $sString
     * @param    Integer    $iMaxLength
     *
     * @return    String
     */
    public static function filterAlpha($sString, $iMaxLength=-1)
    {
        preg_match("/^[A-Za-z]+$/", $sString, $aMatchs);

        if (!empty($aMatchs)) {
            return ($iMaxLength>0)? substr($iMaxLength, 0, $iMaxLength) : $aMatchs[0];
        } else {
            return false;
        }
    }

    /**
     * FileName Filter
     *
     * <code>
     * $sFileName = utilXssVar::filterFileName('filename.xml');
     * </code>
     *
     * @param    String    $sFileName
     * @param    Integer    $iMaxLength
     *
     * @return    String
     */
    public static function filterFileName($sFileName, $iMaxLength=-1)
    {
        preg_match("/^[\.\-\s#_a-zA-Z\d]+$/", $sFileName, $aMatchs);

        if (!empty($aMatchs)) {
            return ($iMaxLength>0)? substr($iMaxLength, 0, $iMaxLength) : $aMatchs[0];
        } else {
            return false;
        }
    }

    /**
     * Direcotry Path Filter
     *
     * <code>
     * $sPath = utilXssVar::filterPath('/home/sdk');
     * </code>
     *
     * @param    String    $sPath
     * @param    Integer    $iMaxLength
     *
     * @return    String
     */
    public static function filterPath($sPath, $iMaxLength=-1)
    {
        //preg_match("/^[\.\-\s_a-zA-Z\d][\/\.\-\s_a-zA-Z\d]*$/", $sPath, $aMatchs);
        //preg_match("/^[\/\-\s_a-zA-Z\d][\/\-\s_a-zA-Z\d]*$/", $sPath, $aMatchs);
        preg_match("/^[A-Z\:\\\\\/|\\\\\/]+[\-\s_a-zA-Z\d][\\\\\/\-\s_a-zA-Z\d]*$/", $sPath, $aMatchs);
        if (!empty($aMatchs)) {

            preg_match("/\/\/|\.\./", $aMatchs[0], $aCatch);

            if (!empty($aCatch))    return false;

            return ($iMaxLength>0)? substr($iMaxLength, 0, $iMaxLength) : $aMatchs[0];
        } else {
            return false;
        }
    }

    /**
     * Alphabetics and Space[ ] Filter
     *
     * <code>
     * $sString = utilXssVar::filterAlphaSpace('abc def');
     * </code>
     *
     * @param    String    $sString
     * @param    Integer    $iMaxLength
     *
     * @return    String
     */
    public static function filterAlphaSpace($sString, $iMaxLength=-1)
    {
        preg_match("/^[A-Za-z]([A-Za-z\s]*[A-Za-z])*$/", $sString, $aMatchs);

        if (!empty($aMatchs)) {
            return ($iMaxLength>0)? substr($iMaxLength, 0, $iMaxLength) : $aMatchs[0];
        } else {
            return false;
        }
    }

    /**
     * Alphabetics and Number Filter
     *
     * <code>
     * $sString = utilXssVar::filterAlphaNum('abc123');
     * </code>
     *
     * @param    String    $sString
     * @param    Integer    $iMaxLength
     *
     * @return    String
     */
    public static function filterAlphaNum($sString, $iMaxLength=-1)
    {
        preg_match("/^[A-Za-z][A-Za-z0-9]*$/", $sString, $aMatchs);

        if (!empty($aMatchs)) {
            return ($iMaxLength>0)? substr($iMaxLength, 0, $iMaxLength) : $aMatchs[0];
        } else {
            return false;
        }
    }

    /**
     * Alphabetics and Number and Space[ ] Filter
     *
     * <code>
     * $sString = utilXssVar::filterAlphaNumSpace('abc 123');
     * </code>
     *
     * @param    String    $sString
     * @param    Integer    $iMaxLength
     *
     * @return    String
     */
    public static function filterAlphaNumSpace($sString, $iMaxLength=-1)
    {
        preg_match("/^[A-Za-z]([A-Za-z0-9\s]*[A-Za-z0-9])*$/", $sString, $aMatchs);

        if (!empty($aMatchs)) {
            return ($iMaxLength>0)? substr($iMaxLength, 0, $iMaxLength) : $aMatchs[0];
        } else {
            return false;
        }
    }

    /**
     * Alphabetics and Number and Underline[_] Filter
     *
     * <code>
     * $sString = utilXssVar::filterAlpha_Num('abc_123');
     * </code>
     *
     * @param    String    $sString
     * @param    Integer    $iMaxLength
     *
     * @return    String
     */
    public static function filterAlpha_Num($sString, $iMaxLength=-1)
    {
        preg_match("/^[A-Za-z]([A-Za-z0-9_]*[A-Za-z0-9])*$/", $sString, $aMatchs);

        if (!empty($aMatchs)) {
            return ($iMaxLength>0)? substr($iMaxLength, 0, $iMaxLength) : $aMatchs[0];
        } else {
            return false;
        }
    }

    /**
     * Alphabetics and Number and Underline[_] and Space[ ] Filter
     *
     * <code>
     * $sString = utilXssVar::filterAlpha_Num('abc_ 123');
     * </code>
     *
     * @param    String    $sString
     * @param    Integer    $iMaxLength
     *
     * @return    String
     */
    public static function filterAlpha_NumSpace($sString, $iMaxLength=-1)
    {
        preg_match("/^[A-Za-z]([A-Za-z0-9\s_]*[A-Za-z0-9])*$/", $sString, $aMatchs);

        if (!empty($aMatchs)) {
            return ($iMaxLength>0)? substr($iMaxLength, 0, $iMaxLength) : $aMatchs[0];
        } else {
            return false;
        }
    }

    /**
     * Alphabetics or Number Filter
     *
     * <code>
     * $sString = utilXssVar::filterAlphaOrNum('abc');
     * $sString = utilXssVar::filterAlphaOrNum('123');
     * </code>
     *
     * @param    String    $sString
     * @param    Integer    $iMaxLength
     *
     * @return    String
     */
    public static function filterAlphaOrNum($sString, $iMaxLength=-1)
    {
        preg_match("/^[A-Za-z0-9]([A-Za-z0-9\s]*[A-Za-z0-9])*$/", $sString, $aMatchs);

        if (!empty($aMatchs)) {
            return ($iMaxLength>0)? substr($iMaxLength, 0, $iMaxLength) : $aMatchs[0];
        } else {
            return false;
        }
    }

    /**
     * String Filter
     *
     * <code>
     * $sString = utilXssVar::filterAlphaOrNum('abc()/'",.-$&_abcdABCD');
     * </code>
     *
     * @param    String    $sString
     * @param    Integer    $iMaxLength
     *
     * @return    String
     */
    public static function filterString($sString, $iMaxLength=-1)
    {
        //preg_match("/^[\(\)\/\'\"\,\.\-\$\&\?s@\?#_a-zA-Z\d]+$/", $sString, $aMatchs);
        preg_match("/^[\(\)\/\'\"\,\.\-\$\&\?s\?#_a-zA-Z\d\s]+$/", $sString, $aMatchs);

        if (!empty($aMatchs)) {
            return ($iMaxLength>0)? substr($iMaxLength, 0, $iMaxLength) : $aMatchs[0];
        } else {
            return false;
        }
    }
}
